<template>
  <van-cell-group>
    <van-field v-model="nick_name" placeholder="修改昵称"></van-field>
  </van-cell-group>
</template>

<script>
  import {CellGroup, Field, Toast} from 'vant'

  export default {
    name: "Nickname",
    components: {
      [CellGroup.name]: CellGroup,
      [Field.name]: Field,
      [Toast.name]: Toast,
    },
    data() {
      return {
        nick_name: ""
      }
    },
    props: ["icon"],
    methods: {
      setMsg() {
        let _this = this;
        if (_this.nick_name === "") {
          Toast.fail('请输入昵称！');
          _this.returnFlag(false);
          return;
        }
        let data = _this.$qs.stringify({
          'nick_name': _this.nick_name
        });
        let token = _this.getCookie("token");
        _this.$http.post(_this.apiUrl("user/users/setInfo"), data, {headers: {'token': token}}).then((res) => {
          // console.log(res.data);
          let code = res.data.statusCode;
          if (code === 200) {
            Toast.success('修改成功！');
            _this.returnFlag(true);
          }
          else {
            _this.returnFlag(false);
            Toast.fail('修改失败！');
          }
        });
      },
      returnFlag(flag) {
        let _this = this;
        if (flag) {
          _this.$emit("giveFlag", _this.randomWord(true, 10, 20));
        }
        else {
          _this.$emit("giveFlag", "null");
        }
      },
    },
    mounted() {
      let _this = this;
      setTimeout(function () {
        _this.nick_name = _this.icon;
      }, 300);
    }
  }
</script>

<style scoped>

</style>
